package com.qk.command.impl;

import java.util.regex.Pattern;

import com.qk.Dao.PlayerDao;
import com.qk.command.Command;
import com.qk.my.pojos.Player;
import com.qk.util.MyConsole;

public class SignUpCommand implements Command {

	public void execute() {
		String nickname, username, password;

		nickname = MyConsole.askUserInput("昵称");
		while (true) {
			username = MyConsole.askUserInput("用户名");
			if (PlayerDao.isExist(username)) {
				MyConsole.println("用户名已经存在，请重新输入！");
				continue;
			}
			try {
				voliDatePassword(username);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				MyConsole.println("error>用户名格式不对：" + e.getMessage());
				continue;
			}
			while (true) {
				password = MyConsole.askUserInput("密码");

				break;
			}
			Player player = new Player();
			player.setName(nickname);
			player.setPassword(password);
			player.setUsername(username);
			int i = PlayerDao.insertPlayer(player);
			if (i == 1) {
				MyConsole.println("===========" + player.getName() + "注册成功");
			}
			break;
		}
	}

	private void voliDatePassword(String password) throws Exception {
		Pattern pat = Pattern.compile("[0-9a-zA-Z]{6,}");
		if (!pat.matcher(password).find()) {
			throw new Exception("用户名长度必须大于等于6");
		}

		Pattern pat1 = Pattern.compile("[0-9]");
		if (!pat1.matcher(password).find()) {
			throw new Exception("用户名中必须包含一个数字");
		}

		Pattern pat2 = Pattern.compile("[a-zA-Z]");
		if (!pat2.matcher(password).find()) {
			throw new Exception("用户名中必须包含一个字母");
		}

	}
}
